/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webserver;

import java.util.ArrayList;
import java.util.TimerTask;

/**
 * CacheInvalidator class checks the whole Cache for invalid entries
 * The execution of this is scheduled periodically
 * 
 */
public class PreCaching extends TimerTask {

	// Variables
	private Cache cache;
	private CacheManagement cm;

	/**
	 * PreCaching Default Constructor
	 * @param cm - CacheManagement
	 */
	public PreCaching(CacheManagement cm) {
		this.cache = Cache.getInstance();
		this.cm = cm;
	}

	/**
	 * The run Method will check for Items to preCache
	 */
	@Override
	public void run() {
		ArrayList<Request> checkPreCaching = cm.getPreCachingRequests();

		for (Request r : checkPreCaching) {
			String absPath = r.getAbsPath();
			absPath = absPath.replace('/', '\\');
			if (!this.cache.isCached(absPath)) {
				this.cache.cache(absPath);
			}
		}
	}
}
